package com.syqc.comlib.utils

import android.app.Activity
import android.content.Context
import android.content.Intent
import androidx.activity.ComponentActivity
import androidx.activity.result.ActivityResult
import androidx.activity.result.ActivityResultCallback
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.fragment.app.Fragment

/**
 *@Author pc_hfjs
 *@Date 2022/4/8 14:41
 *@Desc
 */
object ActivityResultUtil {
    fun ComponentActivity.registerResult(callback: ActivityResultCallback<ActivityResult>): ActivityResultLauncher<Intent> {
        return this.registerForActivityResult(
            ActivityResultContracts.StartActivityForResult(),
            callback
        )
    }

    fun ComponentActivity.registerResultOk(callback: ActivityResultCallback<ActivityResult>): ActivityResultLauncher<Intent> {
        return this.registerForActivityResult(
            ActivityResultContracts.StartActivityForResult()
        ) {
            if (it.resultCode == Activity.RESULT_OK) {
                callback.onActivityResult(it)
            }
        }
    }

    inline fun ActivityResultLauncher<Intent>.launchIntent(block: Intent.() -> Unit) {
        val intent = Intent()
        intent.block()
        launch(intent)
    }

    inline fun <reified T> ActivityResultLauncher<Intent>.launchAct(
        context: Context,
        block: Intent.() -> Unit
    ) {
        val intent = Intent(context, T::class.java)
        intent.block()
        launch(intent)
    }

    inline fun <reified T> ActivityResultLauncher<Intent>.launchAct(context: Context) {
        val intent = Intent(context, T::class.java)
        launch(intent)
    }

    fun Fragment.registerResult(callback: ActivityResultCallback<ActivityResult>): ActivityResultLauncher<Intent> {
        return this.registerForActivityResult(
            ActivityResultContracts.StartActivityForResult(),
            callback
        )
    }

    fun Fragment.registerResultOk(callback: ActivityResultCallback<ActivityResult>): ActivityResultLauncher<Intent> {
        return this.registerForActivityResult(
            ActivityResultContracts.StartActivityForResult()
        ) {
            if (it.resultCode == Activity.RESULT_OK) {
                callback.onActivityResult(it)
            }
        }
    }


}

/**
 * reified  泛型实例化
 * 高级函数
 */
inline fun <reified T> startActivityTo(context: Context, block: Intent.() -> Unit) {
    val intent = Intent(context, T::class.java)
    intent.block()
    context.startActivity(intent)
}

inline fun Class<*>.startActivityTo(context: Context, block: Intent.() -> Unit) {
    val intent = Intent(context, this)
    intent.block()
    context.startActivity(intent)
}

/**
 * reified  泛型实例化
 * 高级函数
 */
inline fun <reified T> startActivityTo(context: Context) {
    val intent = Intent(context, T::class.java)
    context.startActivity(intent)
}


/**
 * reified  泛型实例化
 * 高级函数
 */
inline fun <reified T> startServiceTo(context: Context, block: Intent.() -> Unit) {
    val intent = Intent(context, T::class.java)
    intent.block()
    context.startService(intent)
}

inline fun setResultTo(
    context: Activity,
    resultCode: Int = Activity.RESULT_OK,
    block: Intent.() -> Unit
) {
    val intent = Intent()
    intent.block()
    context.setResult(resultCode, intent)
}

fun setResultTo(
    context: Activity,
    resultCode: Int = Activity.RESULT_OK
) {
    val intent = Intent()
    MLog.e("打印========$resultCode")
    context.setResult(resultCode, intent)
}